package br.com.salaodebeleza.view;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import br.com.salaodebeleza.config.AppPadraoConfig;
import br.com.salaodebeleza.security.MyCredentials;
import br.gov.frameworkdemoiselle.security.SecurityContext;
import br.gov.frameworkdemoiselle.stereotype.ViewController;

@ViewController
public class LoginMB implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Inject
	SecurityContext context;
	@Inject
	MyCredentials credentials;
	@Inject
	private FacesContext facesContext;
	@Inject 
	private AppPadraoConfig config;
	
	private String username;

	private String password;

	private boolean logado;

	public String loginResult() {

		credentials.setUsername(username);
		credentials.setPassword(password);
		credentials.setSite(config.getSiteCodigo());
		context.login();
		
		if (context.isLoggedIn()) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
					"Erro: Usuario Senha incorretos ou sem acesso ao sistema."));
			return "/index.jsf";
		} else {
			facesContext.addMessage("formLogin", new FacesMessage(
					"Erro: Usuario Senha incorretos ou sem acesso ao sistema."));
			return "/login.jsf";
		}
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isLogado() {
		return logado;
	}

}
